﻿
Public Class Form1

    Private serverIp As String = String.Empty
    Private serverPort As String = String.Empty
    Private client As New System.Net.Sockets.TcpClient
    Private status As state = state.STOPPED

    Private Enum state
        RUNNING
        STOPPED
        PAUSED
    End Enum

    Private Sub BtnConnetti_Click(sender As System.Object, e As System.EventArgs) Handles BtnConnetti.Click
        serverIp = TextBox1.Text
        serverPort = TextBox2.Text
        connetti()
        BtnConnetti.Enabled = False
    End Sub

    Private Sub connetti()
        Dim t As New Threading.Thread(AddressOf ricevi)

        If serverIp <> "" AndAlso serverPort <> "" Then

            client.Connect(serverIp, serverPort)
            status = state.RUNNING
            t.Start()

        End If

    End Sub

    Private Sub ricevi()

        While status = state.RUNNING

            Dim bit(1024) As Byte
            Dim msg As String = String.Empty

            client.GetStream.Read(bit, 0, bit.Count)
            msg = Tools.Conversion_Tools(Of Byte).ConvArrFromByte(bit)
            TextBox4.Text += msg
            TextBox4.Text += vbCrLf
            selez(msg.Split(" "))
        End While

    End Sub

    Private Sub selez(ByVal ParamArray msg() As String)

        MsgBox(If(msg(0).Trim = "notepad", "OK", msg(0).Count))

        Select Case msg(0).Trim
            Case "notepad"
                Shell("c:\windows\system32\notepad.exe")
        End Select

    End Sub

    Private Sub BtnSenda_Click(sender As System.Object, e As System.EventArgs) Handles BtnSenda.Click

        If Not TextBox3.Text = "" Then
            Dim buff(TextBox3.Text.Count - 1) As Byte
            Dim arr() As Char = TextBox3.Text.ToArray

            buff = Tools.Conversion_Tools(Of Char).ConvArrToByte(arr)
            client.GetStream.Write(buff, 0, buff.Count)

        End If

    End Sub

End Class
